﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace Kevin.SLUserControlLibrary
{
    public class FriendItem : ButtonBase
    {

        public static readonly DependencyProperty IsCheckedProperty = DependencyProperty
            .Register("IsChecked", typeof(bool), typeof(FriendItem),
            new PropertyMetadata(new PropertyChangedCallback(FriendItem.OnIsCheckedChanged)));

        public bool IsChecked
        {
            get { return (bool)GetValue(FriendItem.IsCheckedProperty); }
            set { SetValue(FriendItem.IsCheckedProperty, value); }
        }

        private StackPanel InfoPanelElement;

        public FriendItem()
        {
            this.DefaultStyleKey = typeof(FriendItem);
        }
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            InfoPanelElement = base.GetTemplateChild("InfoPanelElement") as StackPanel;
        }

        protected override void OnMouseEnter(MouseEventArgs e)
        {
            base.OnMouseEnter(e);
            this.ChangeVisualState(true);
        }

        protected override void OnMouseLeave(MouseEventArgs e)
        {
            base.OnMouseLeave(e);
            this.ChangeVisualState(true);
        }

        protected override void OnClick()
        {
            base.OnClick();
            this.IsChecked = true;
            this.ChangeVisualState(true);
        }

        private static void OnIsCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            FriendItem friendItem = (FriendItem)d;
            friendItem.ChangeVisualState(true);
        }

        private void ChangeVisualState(bool useTransitions)
        {
            if (!base.IsEnabled)
            {
                VisualStateManager.GoToState(this, "Disabled", useTransitions);
            }
            else if (this.IsChecked)
            {
                VisualStateManager.GoToState(this, "Checked", useTransitions);
                this.InfoPanelElement.Orientation = Orientation.Vertical;
            }
            else if (base.IsMouseOver)
            {
                VisualStateManager.GoToState(this, "MouseOver", useTransitions);
            }
            else
            {
                VisualStateManager.GoToState(this, "Normal", useTransitions);
                this.InfoPanelElement.Orientation = Orientation.Horizontal;
            }
        }
    }
}
